package com.angcyo.selenium

import org.openqa.selenium.Dimension
import org.openqa.selenium.Point
import org.openqa.selenium.Rectangle
import org.openqa.selenium.WebElement
import org.openqa.selenium.interactions.Coordinates
import org.openqa.selenium.remote.RemoteWebElement

/**
 * 特殊含义的元素
 *
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2020/12/30
 */

/**驱动元素*/
class DriverWebElement(val windowRect: Rectangle) : PlaceholderWebElement() {

    init {
        setId("DriverWebElement")
    }

    override fun toString(): String {
        return "驱动根元素[DriverWebElement]"
    }

    override fun getLocation(): Point {
        return Point(windowRect.x, windowRect.y)
    }

    override fun getSize(): Dimension {
        return Dimension(windowRect.width, windowRect.height)
    }

    override fun getRect(): Rectangle {
        return windowRect
    }
}

fun WebElement.isDriverWebElement() = this is DriverWebElement

/**log保持*/
class LogWebElement(val log: String) : PlaceholderWebElement() {

    init {
        setId("LogWebElement")
    }

    override fun toString(): String {
        return log
    }
}

/**占位元素*/
open class PlaceholderWebElement : RemoteWebElement() {

    init {
        setId("PlaceholderWebElement")
    }

    override fun toString(): String {
        return "占位元素[PlaceholderWebElement]"
    }
}

class LocatableWebElement(val rectangle: Rectangle) : PlaceholderWebElement() {
    init {
        setId("LocatableWebElement")
    }

    override fun getRect(): Rectangle {
        return rectangle
    }

    override fun getSize(): Dimension {
        return Dimension(rectangle.width, rectangle.height)
    }

    override fun getLocation(): Point {
        return Point(rectangle.x, rectangle.y)
    }

    override fun getCoordinates(): Coordinates {
        return object : Coordinates {
            override fun onScreen(): Point {
                return Point(rectangle.x, rectangle.y)
            }

            override fun inViewPort(): Point {
                return onScreen()
            }

            override fun onPage(): Point {
                return onScreen()
            }

            override fun getAuxiliary(): Any {
                return this
            }
        }
    }
}

